package com.i2bt.app.login.model;

import org.json.JSONException;
import org.json.JSONObject;

import android.widget.Toast;

import com.i2bt.app.BaseActivity;
import com.i2bt.app.BaseJsonModel;
import com.i2bt.app.login.RegistActivty;
import com.i2bt.bean.RegistBean;
import com.i2bt.common.AndroidRestContract;

public class RegistJsonHandle extends BaseJsonModel {

	RegistActivty act;

	public RegistJsonHandle(BaseActivity activity) {
		super(activity);
		act = (RegistActivty) activity;
	}

	@Override
	protected String logicToWrap() {
		// TODO Auto-generated method stub
		RegistBean bean = (RegistBean) this.beanParam;
		try {
			jsonParam.put(AndroidRestContract.ACTION, bean.getAct());
			jsonParam.put(AndroidRestContract.USER_ID, bean.getUserId());
			jsonParam.put(AndroidRestContract.NICK_NAME, bean.getNickName());
			jsonParam.put(AndroidRestContract.PASSWORD, bean.getPassword());
			jsonParam.put(AndroidRestContract.EMAIL, bean.geteMail());
			jsonParam.put(AndroidRestContract.PHONE, bean.getPhone());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return jsonParam.toString();
	}

	@Override
	protected void logicToUnwrap(boolean isSuc, String result) {
		System.out.println(result);
		if (isSuc) {
			try {
				JSONObject ja = new JSONObject(result);
				boolean succ = ja.getBoolean(AndroidRestContract.SUCCEED);
				String mes = null;
				mes = ja.getString(AndroidRestContract.MESSAGE);
				if (succ) {
					Toast.makeText(act, "注册成功!", Toast.LENGTH_SHORT).show();
					try {
						Thread.sleep(Toast.LENGTH_SHORT);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					this.act.finish();
				} else {
					act.showWarnToast(mes);
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			act.showWarnToast("连接失败");
		}
	}

}
